package org.gd.metrics;

import com.codahale.metrics.*;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

public class QueueManager {

    private MetricRegistry metrics = new MetricRegistry();

    public QueueManager() {
        MetricRegistry.name(Queue.class, "requests", "size");
        MetricRegistry.name(Queue.class, "responses", "size");

        Counter counter = new MetricRegistry().counter("counter");
        counter.dec();

        System.out.println(new MetricRegistry().getCounters().get("counter").getCount());

        Meter meter = metrics.meter("meter");
        meter.mark();

        System.out.println("meter.getFifteenMinuteRate() : " + meter.getFifteenMinuteRate());
        System.out.println("meter.getCount() : " + meter.getCount());
        System.out.println("meter.getFiveMinuteRate() : " + meter.getFiveMinuteRate());
        System.out.println("meter.getMeanRate() : " + meter.getMeanRate());
        System.out.println("meter.getOneMinuteRate() : " + meter.getOneMinuteRate());



        Gauge<Integer> gauge = metrics.register("register", new Gauge<Integer>() {
            @Override
            public Integer getValue() {
                Double aDouble = Math.random();
                return aDouble.intValue();
                //return cache.getEvictionsCount();
            }
        });

        System.out.println(gauge.getValue());

        Gauge newGauge = metrics.getGauges().get("register");

        System.out.println(newGauge.getValue());







        Timer timer = new Timer();
        //try { Thread.sleep(100L); } catch (InterruptedException e) {}
        System.out.println(new SimpleDateFormat("mm:ss.S").format(new Date(timer.time().stop())));


    }

    @Timed
    public Integer getInteger() {
        try {
            Double aDouble = Math.random() * 20D;
            Thread.sleep(aDouble.longValue());
            return aDouble.intValue();
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
